// 用户信息
var userInfo = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user;

// 客户组ID
var mainCustomerId = sessionStorage.getItem("mainCustomerId");

//页大小
var pageSize = 50;

//项目映射-列表
function projectMapPageList () {
    var json = {
        mainCustomerId: parseInt(mainCustomerId),
        crmCustomerCode: $("#crmCustomerCode").val(),
        crmCustomeName: $("#crmCustomeName").val(),
        crmCustomerId: $("#crmCustomerId").val(),
        projectCode: $("#projectCode").val(),
        projectName: $("#projectName").val(),
        mappingStatus: $("#mappingStatus").val(),
        pageNum: 1,
        pageSize: pageSize
    }
    $.ajax({
        url: root.projectMap.queryListPage,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(json),
        success: function (result) {
            projectMap_Shield(result);
            $('.M-box3-project-Map').pagination({
                pageCount: result.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    json.pageNo = api.getCurrent();
                    $.ajax({
                        url: root.projectMap.queryListPage,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType: 'json',
                        contentType: "application/json",
                        data: JSON.stringify(json),
                        success: function (result) {
                            projectMap_Shield(result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！', 2000);
        }
    });
}

//项目映射-列表-渲染
function projectMap_Shield (dataSource) {
    var dttable = $('#projectMapDataTable').dataTable(); // 初始化dataTable，清空上一个值
    dttable.fnClearTable(); //清空一下table
    dttable.fnDestroy(); //还原初始化了的datatable
    $("#projectMapDataTable").DataTable({
        "order": [[2, "desc"]],
        "autoWidth": false,
        "ordering": false,
        "searching": false,
        "paging": false,
        "retrieve": true,
        "destroy": true,
        "bAutoWidth": true,
        "bLengthChange": true,
        "aLengthMenu": [5, 10, 25, 50],
        "oLanguage": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "抱歉， 没有找到",
            "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
            "sInfoEmpty": "",
            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            },
            "sZeroRecords": "没有检索到数据",
            "sProcessing": "<img src='./loading.gif' />",
        },
        data: dataSource.data.data,
        columnDefs: [
            {
                "render": function (data, type, row, meta) {
                    var val = row.id
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 0
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.crmCustomerId
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 1
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.crmCustomerCode
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 2
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.crmCustomeName
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 3
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.crmCustomerBelong
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 4
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.projectCode
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 5
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.projectName
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 6
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.mappingStatus == 1 ? "已映射" : "未映射";
                    return val;
                },
                "targets": 7
            },
            {
                "render": function (data, type, row) {
                    var val = "<a href='javascript:0;' onclick=\"skipPage('项目映射编辑','" + row.projectCode  + "')\">编辑</a> ";
                    val += " <a href = 'javascript:0;' onclick=\"remove_Product_Map(" + row.id + ",'" + row.projectCode + "')\" >解除映射</a>";
                    return val;
                },
                "targets": 8
            },
        ]
    });
};

//重置
function projectMapReset () {
    $("#crmCustomerCode").val("");
    $("#crmCustomeName").val("");
    $("#crmCustomerId").val("");
    $("#projectCode").val("");
    $("#projectName").val("");
    $("#mappingStatus").val("");
    projectMapPageList();
}


//下载渠道重点商品关联模板
function exportProjectsMappingErrorExcel () {
    window.location.href = root.projectMap.exportProjectsMappingErrorExcel + "?operateUserId=" + userInfo.user_ID + "&mainCustomerId=" + mainCustomerId;
}

//项目映射-导出
function exportProjectsMappingExcel () {
    window.location.href = root.projectMap.exportProjectsMappingExcel + "?operateUserId=" + userInfo.user_ID + "&operateUserName=" + userInfo.name + "&mainCustomerId=" + mainCustomerId;
}

//项目映射-导出模板下载
function templateProjectsMappingExcel () {
    window.location.href = root.projectMap.templateProjectsMappingExcel + "?urlStr=https://fsyuncai-file.oss-cn-beijing.aliyuncs.com/common/20210910144149150987.xls&fileName=渠道项目导入-模板";
}

//项目映射-批量导入
function importProjectsMappingExcel () {
    // var name = $("#file").val();
    // var result = /\.[^\.]+/.exec(name);
    // if (result != ".xlsx" && result != ".xls") {
    //     $.Huimodalalert('格式不正确！', 2000);
    //     return false;
    // }
    var formData = new FormData();
    formData.append('file', $("#file")[0].files[0]);
    formData.append('operateUserId', userInfo.user_ID);
    formData.append('operateUserName', userInfo.name);
    formData.append('mainCustomerId', parseInt(mainCustomerId));
    formData.append('operateAcount', userInfo.username);
    $.ajax({
        url: root.projectMap.importProjectsMappingExcel,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function (data) {
            console.log(data)
            $('#file').val('');
            if (data.code == 0) {
                $.Huimodalalert('导入成功', 2000);
            } else {
                $.Huimodalalert(data.msg, 2000);
            }
        }
    });
}

//跳转
function skipPage (title, projectCode) {
    var index = layer.open({
        type: 2,
        title: title,
        content: "../accountAystem/projectMap/projectMapEdit.html"
    });
    layer.full(index);

    sessionStorage.setItem("projectCode", projectCode);
}

//解除映射
function remove_Product_Map (id, projectCode) {
    var p_json = {
        id: id,
        projectCode: projectCode
    }

    layer.open({
        type: 1,
        title: "提示",
        offset: "t",
        content: "<div style='margin: 20px;'>请确认是否解除</div>",
        area: "620px",
        btn: ["确认", "取消"],
        btnAlign: "c",
        yes: function () {
            layer.close(layer.index);
            $.ajax({
                url: root.projectMap.removeCustomerProjects,
                cache: false,  //禁用缓存
                type: "POST",
                dataType: 'json',
                contentType: "application/json",
                data: JSON.stringify(p_json),
                success: function (result) {
                    if (result.code == 0) {
                        projectMapPageList();
                        $.Huimodalalert('解除成功！', 2000);
                    }
                    else {
                        $.Huimodalalert("'提交失败" + result.msg, 2000);
                    }
                },
                error: function () {
                    $.Huimodalalert('提交失败！', 2000);
                }
            });
        },
        cancel: function () {

        },
    });
}